package com.hcj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcj.pojo.Category;
import com.hcj.service.CategoryService;
import com.hcj.util.MyResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService service;

    // 添加商品名称
    @RequestMapping(value = "/add")
    MyResponse add(Category category) {
        category.setCreateTime(new Timestamp(new Date().getTime()));
        return service.add(category);
    }

    // 修改商品类别信息
    @RequestMapping(value = "/update", params = {"id"})
    MyResponse update(Category category){
        return service.update(category);
    }

    // 删除一个商品类别
    @RequestMapping(value = "/delete", params = {"name"})
    MyResponse delete(String name){
        return service.delete(name);
    }

    // 查询一个商品类别
    @RequestMapping(value = "/getOne")
     Page<Category> selectOneByName(String name,Integer currentPage,Integer pageSize){
        currentPage = currentPage == null ? 1 : currentPage;
        pageSize = pageSize == null ? 5 : pageSize;
        if (name.equals("")){
            return service.selectAllPage(currentPage,pageSize);
        }
        return service.selectOneByName(name,currentPage,pageSize);
    }

    @RequestMapping("/selectAll")
    public Page<Category> selectAll(Integer currentPage,Integer pageSize){

        return service.selectAllPage(currentPage,pageSize);
    }
}
